//
//  HWIndexViewController.m
//  嗨文
//
//  Created by 响当当 on 5/21/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HWIndexViewController.h"
#import "HWBookIndexView.h"
#import "TempViewController.h"

@interface HWIndexViewController ()
@property (nonatomic, strong)HWBookIndexView *bookIndexView;
@end

@implementation HWIndexViewController

- (HWBookIndexView *)bookIndexView
{
    if (!_bookIndexView)
    {
        _bookIndexView = [[HWBookIndexView alloc]initWithFrame:CGRectMake(0,NAVIBAR_HEIGHT,SCREEN_WIDTH,SCREEN_HEIGHT-NAVIBAR_HEIGHT)];
        
        WeakSelf(self)
        _bookIndexView.upLoadIndexBlock = ^(NSString *page, NSString *size) {
            [weakSelf getIndexInfo:weakSelf.booID page:page size:size];
        };
        
        _bookIndexView.loadChapterBlock = ^(NSString *chapterId, NSString *chapterCoin) {
            TempViewController *vc = [[TempViewController alloc]init];
            vc.chapterId = chapterId;
            vc.chapterCoin = chapterCoin;
            vc.chapterFreeNum = @"20";
            vc.hidesBottomBarWhenPushed = YES;
            [[HWUtilties getCurrentVC].navigationController pushViewController:vc animated:YES];
        };
    }
    return _bookIndexView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setDefaultNavWithTitle:@"目录"];
    
    [self.view addSubview:self.bookIndexView];
    
    [self getIndexInfo:self.booID page:@"1" size:@"30"];
    
}

#pragma mark - 加载目录
- (void)getIndexInfo:(NSString *)bookId page:(NSString *)page size:(NSString *)size
{
    NSDictionary *prarmers = @{
                               @"access_token" : current_User.token,
                               @"bookId" : bookId,
                               @"pageNum" : page,
                               @"pageSize" : size
                               };
    
    WeakSelf(self)
    [MyInfoManager getBookIndexInfo:prarmers sucess:^(id  _Nonnull acceptData)
     {
         NSNumber *nub = IFNULL(acceptData[@"hasNextPage"], [NSNumber new]);
         
         [weakSelf.bookIndexView getDate:acceptData[@"list"] hasNetPage:nub];
         
     } fail:^(NSError * _Nullable error) {
         DISMISS_SVP(1.0f);
         [SVProgressHUD showErrorWithStatus:@"获取失败，请检查网络是否良好"];
         DISMISS_SVP(1.0f);
     }];
}

@end
